package cn.yunhe.actions;

import cn.yunhe.model.House;
import cn.yunhe.service.HouseBiz;
import cn.yunhe.util.BaseActionUtil;
import cn.yunhe.util.ModelDisplayUtil;
import cn.yunhe.util.Pagination;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*spring注解*/
@Controller(value = "houseAction")
@Scope(value = "prototype")
/*struts2注解*/
/*@ParentPackage(value = "struts-default")*/
@ParentPackage(value = "json-default")
public class HouseAction extends BaseActionUtil implements ModelDriven {

    private House house;
    private Pagination<House>pagination=new Pagination<>();

    private int totalRecords;//总记录数,充要条件
    private int maxRows;//每页显示的最大行数,,充要条件
    private int currPageNo;//当前页码,,充要条件

    List<House>houseList=new ArrayList<>();

    /*spring注解*/
    @Autowired
    @Qualifier("houseBiz")
    HouseBiz houseBiz;

    /*struts2注解*/
    @Action(value = "house_searchHouse",results = {
            @Result(name = "success",location = "/searchHouse.jsp"),
            @Result(name = "error",location = "/fail2SearchHouse.jsp"),
    })
    public String searchHouse(){
        //此处的House1对应
        //@Entity(name = "House1")
        //@Table(name = "house")
        List<House>houseList=houseBiz.searchHouse("from House1");
        getRequest().setAttribute("houseList1",houseList);

        ModelDisplayUtil.listHouse(houseList);
        return SUCCESS;
    }

    /*struts2注解*/
    @Action(value = "house_searchHouseByConditions",results = {
            @Result(name = "houseList1",type ="json",params = {"root","houseList"}),
    })
    public String searchHouseByConditions(){
        pagination.setTotalRecords(totalRecords);
        pagination.setMaxRows(maxRows);
        pagination.setCurrPageNo(currPageNo);
        System.out.println("========house&&pagination========");
        System.out.println(pagination.getTotalRecords());
        System.out.println(pagination.getMaxRows());
        System.out.println(pagination.getCurrPageNo());
        ModelDisplayUtil.singleHouse(house);
        System.out.println("========house&&pagination END========");
        /*使用ajax异步技术接收JSON数据*/
        houseList= houseBiz.searchHouse(house,pagination);
        System.out.println("===========houseList=============");
        ModelDisplayUtil.listHouse(houseList);
        System.out.println("=============houseList END===========");
        return "houseList1";
    }


    public HouseBiz getHouseBiz() {
        return houseBiz;
    }

    public void setHouseBiz(HouseBiz houseBiz) {
        this.houseBiz = houseBiz;
    }

    public House getHouse() {
        return house;
    }

    public void setHouse(House house) {
        this.house = house;
    }

    public Pagination<House> getPagination() {
        return pagination;
    }

    public void setPagination(Pagination<House> pagination) {
        this.pagination = pagination;
    }

    public List<House> getHouseList() {
        return houseList;
    }

    public void setHouseList(List<House> houseList) {
        this.houseList = houseList;
    }

    public int getTotalRecords() {
        return totalRecords;
    }

    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
    }

    public int getMaxRows() {
        return maxRows;
    }

    public void setMaxRows(int maxRows) {
        this.maxRows = maxRows;
    }

    public int getCurrPageNo() {
        return currPageNo;
    }

    public void setCurrPageNo(int currPageNo) {
        this.currPageNo = currPageNo;
    }

    @Override
    public House getModel() {
        if (house==null)
            house=new House();
        return house;
    }
}
